package Month11.Day24;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Server1 {
    public static void main(String[] args) {
        try {
            ServerSocket ss = new ServerSocket(9999);
            System.out.println("服务器启动，监听在11010口");
            List<User> list = new ArrayList<>();
            User u1 = new User("java1", "123");
            User u2 = new User("java2", "123");
            list.add(u1);
            list.add(u2);
            while (true) {
                Socket s = ss.accept();
                System.out.println("ip:" + s.getInetAddress());
                System.out.println("远程端口：" + s.getPort());
                System.out.println(s.getLocalPort() + "本地");
                System.out.println("socket连接建立了。。。" + s);
                InputStream in = s.getInputStream();
                OutputStream out = s.getOutputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                PrintWriter pw = new PrintWriter(out, true);
                String ID=br.readLine();
                String password=br.readLine();
                boolean flag=false;
                for (User user:list){
                    if (ID.equals(user.getID())&&password.equals(user.getPassword())){
                      flag=true;
                    }
                }if (flag){
                    pw.println("登录成功，欢迎>>>>>>>>>>>>>>>>>>>>>");
                }else {
                    pw.println("登录失败，重新登录>>>>>>>>>>>>>>");
                }

            }


        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
